var addTwoNumbers = function (l1, l2) {
var c = 0;
var ret = new ListNode(0);
var curr = ret;
while (l1 || l2) {
var a = l1 ? l1.val : 0;
var b = l2 ? l2.val : 0;
var sum = a + b + c;
c = Math.floor(sum / 10);
curr.next = new ListNode(sum % 10);
if (l1) {
l1 = l1.next;
}
if (l2) {
l2 = l2.next;
}
curr = curr.next;
}
if (c) {
curr.next = new ListNode(c);
}
return ret.next;
};
def add_two_numbers(l1, l2)
return add(l1,l2,0)
end
def add(l1,l2,f)
if not l1 and not l2 and f==0
return nil
end
ans=f +(l1 ? l1.val : 0)+(l2 ? l2.val : 0)
res=ListNode.new(ans%10)
res.next=add(l1 ? l1.next : nil,l2 ? l2.next : nil,ans/10)
return res
end